let app = null;

function navToNext(event) {
  const that = this;
  let params = event.currentTarget.dataset;
  const url = params.url;
  const unskip = params.unskip;
  const opentype = params.opentype;
  const login = params.login;
  app = getApp();

  if (unskip) return false;

  if(login){
    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;
  }

  for (let key in params) {
    let idx = key.indexOf("__");
    if (idx == -1) continue;
    let joinStr = key.slice(idx, idx + 3);
    let arr = key.split(joinStr);
    if (arr.length < 2) continue;
    params[arr.join(joinStr.toLocaleUpperCase().replace("__", ""))] = params[key];
    delete params[key];
  }

  // if (isInCurrentPages(params)) return false;

  if (opentype == "switchTab") {
    delete params.url;
    delete params.opentype;
    // 跳转到tab页面不能传递参数，规定传递到app.switchTabParams = {};
    app.switchTabParams = params;
    wx.switchTab({
      url: app.xhx_route[url],
    })
    return false;
  }

  wx.navigateTo({
    url: `${app.xhx_route[url]}?${paramFormat(params)}`,
  })
}

function isInCurrentPages(params) {
  let pages = getCurrentPages();
  pages.forEach((item,index) => {
    if (app.xhx_route[params.url].indexOf(item.route) != -1){
      wx.navigateBack({
        delta:pages.length-1-index
      })
      return true;
    }
  })

  return false;
}

function paramFormat(params) {
  let url = '';
  for (let key in params) {
    if (key == 'url' || key == 'unskip') continue;
    if (url) url += `&`;
    url += `${key}=${params[key]}`
  }

  return url;
}

export default {
  navToNext
};